/**
 * Copyright (C), 2020-2020,贵州铭明网络科技有限公司
 * FileName: QuartzController
 * Author:   杨朝湖
 * Date:     2020/12/21 9:56
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.gym.test;

import com.github.pagehelper.Page;
import com.gym.bo.PermJobInfo;
import com.gym.mapper.Quartz;
import com.gym.pojo.JobInfo;
import com.gym.quartz.TestQuartz;
import com.gym.util.PageUtil;
import com.gym.vo.JobAndTrigger;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Gym
 * @create 2020/12/21
 * @since 1.0.0
 */
@RestController
public class QuartzController {

    @Resource
    @Qualifier("Scheduler")
    private Scheduler scheduler;
    @Resource
    private Quartz quartz;

    @GetMapping("/startQuartz")
    public void startQuartz() throws Exception {

        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobClassName("TestQuartz");//设置定时任务类名
        jobInfo.setJobGroupName("TestQuartz");//设置定时任务的组
        jobInfo.setCronExpression("0/2 * * * * ?");//这是定时任务的定时执行corn表达式
        // 启动调度器
        scheduler.start();
        // 构建job信息
        JobDetail jobDetail = JobBuilder
                .newJob(getClass("com.gym.quartz." + jobInfo.getJobClassName()).getClass())
                .withIdentity(jobInfo.getJobClassName(), jobInfo.getJobGroupName())
                .usingJobData("key", "这只是一个参数而已！！！")
                .build();
        // 表达式调度构建器(即任务执行的时间)
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobInfo.getCronExpression())
                .withMisfireHandlingInstructionDoNothing();

        // 按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobInfo.getJobClassName(), jobInfo.getJobGroupName()).withSchedule(scheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

    /**
     * 根据类名称，通过反射得到该类，然后创建一个BaseJob的实例。 由于NewJob和HelloJob都实现了BaseJob，
     * 所以这里不需要我们手动去判断。这里涉及到了一些java多态调用的机制
     *
     * @param classname
     * @return
     * @throws Exception
     */
    public static QuartzJobBean getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (QuartzJobBean) class1.newInstance();
    }

    @PostMapping("/quartzJobList")
    public PageUtil<JobAndTrigger> quartzJobList(@RequestBody PermJobInfo permJobInfo) {
        Integer count = quartz.getJobAndTriggerDetailsCount(permJobInfo);
        List<JobAndTrigger> list = quartz.getJobAndTriggerDetails(permJobInfo);
        PageUtil<JobAndTrigger> page = new PageUtil<>(count, count, permJobInfo.getPageIndex(), permJobInfo.getPageSize(), list);
        return page;
    }
}